package com.yuyou.tas.lesson.controller;

import java.io.InputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import springfox.documentation.annotations.ApiIgnore;

import com.yuyou.tas.lesson.service.IBaseLevelService;
import com.yuyou.tas.lesson.service.ILessonChapterService;
import com.yuyou.tas.lesson.service.ILessonClassStudentService;
import com.yuyou.tas.lesson.service.ILessonService;
import com.yuyou.tas.lesson.service.IUpGradeDataService;
import com.yuyou.tas.lesson.service.comm.OrganLimitService;
import com.yuyou.tas.lesson.vo.req.ReqMergeStuExcel;
import com.yuyou.tas.service.api.admin.service.MailAPI;
import com.yuyou.tas.service.api.lesson.dto.MergeStuDTO;
import com.yuyou.tas.util.bean.LoginRedisObj;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.enums.OrganLimitEnums;
import com.yuyou.tas.util.tool.ListUtil;

@ApiIgnore
@Validated
@RestController
@RequestMapping(value="/api/upgradeDate")
public class UpgradeDateController extends BaseController{
	
	@Autowired
	private IUpGradeDataService upGradeDataService;

	@Autowired
	private ILessonClassStudentService lessonClassStudentService;

	@Autowired
	private IBaseLevelService baseLevelService;
	
	@Autowired
	private ILessonService lessonService;
	
	@Autowired
	private OrganLimitService organLimitService;
	@Autowired
	private ILessonChapterService lessonChapterService;
	
	@RequestMapping(value="/upgradeLesson" ,method=RequestMethod.POST)
	public String upgradeLesson(HttpServletRequest request,Long organId,String key) throws Exception{
		if(!key.equals("!@#$yuyou123321")){
			return "error";
		}
		
	    LoginRedisObj obj = this.getUserObj(request);
	    if(!obj.getIsManager()){
	    	return "not manager";
	    }
		
		upGradeDataService.upgradeLesson(organId,obj.getUserId());
		return "ok";
	}
	
	@RequestMapping(value="/sycnStudent" ,method=RequestMethod.POST)
	public String sycnStudent(HttpServletRequest request,String startTime,String key) throws Exception{
//		if(!key.equals("!@#$yuyou123321")){
//			return "error";
//		}
//	    LoginRedisObj obj = this.getUserObj(request);
//	    if(!obj.getIsManager()){
//	    	return "not manager";
//	    }
		
		lessonClassStudentService.sycnClassStudentData(startTime);
		return "ok";
	}


	@RequestMapping(value="/sycnLevel" ,method=RequestMethod.POST)
	public String sycnLevel(HttpServletRequest request,String code,String key){
		if(!key.equals("!@#$yuyou123321") || code == null){
			return "error";
		}
	    LoginRedisObj obj = this.getUserObj(request);
	    if(!obj.getIsManager()){
	    	return "not manager";
	    }
	    
		baseLevelService.sycnLevel(code , 1l);//写死系统机构为1
		return "ok";
	}


	@RequestMapping(value="/cancelLesson" ,method = {RequestMethod.GET,RequestMethod.POST})
	public String cancelLesson(HttpServletRequest request,String key,Long id) throws Exception{
		if(!key.equals("yuyou1233212")){
			return "error";
		}
		
	    LoginRedisObj obj = this.getUserObj(request);
	    if(!obj.getIsManager()){
	    	return "not manager";
	    }
	    organLimitService.checkOrganByOrganId(obj.getOrganizationId(), id, OrganLimitEnums.type.lesson);
	    ResBoolSimpleInfo<String> res=lessonService.cancelPublicLesson(obj.getOrganizationId(),id);
	    
		return res.getMessage();
	}



	/**
	 * 批量删除旧数据-复制的office文档
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delAllOldChapter" ,method = {RequestMethod.GET,RequestMethod.POST})
	public ReturnResult<String> delAllOldChapter(HttpServletRequest request,String key){
		if(!key.equals("yuyou1233212")){
			return ReturnResult.fail("key错误");
		}

		LoginRedisObj obj = this.getUserObj(request);
		if(!obj.getIsManager()){
			return ReturnResult.fail("你没有权限");
		}
		ResBoolSimpleInfo<String> res=lessonChapterService.delOldData();
		return ReturnResult.returnRes(res);
	}

	/**
	 * 资源type
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sycnResoreceType" ,method = {RequestMethod.GET,RequestMethod.POST})
	public ReturnResult<String> sycnResoreceType(HttpServletRequest request,String key){
		if(!key.equals("yuyou1233212")){
			return ReturnResult.fail("key错误");
		}

		LoginRedisObj obj = this.getUserObj(request);
		if(!obj.getIsManager()){
			return ReturnResult.fail("你没有权限");
		}

    	//TODO 暂时
    	new Thread(new Runnable() {		
			@Override
			public void run() {						
				ResBoolSimpleInfo<String> res=upGradeDataService.sycnResoreceType();
			}
		}).start();
		

		
		return ReturnResult.success();
	}
	
	/**
	 * 批量删除旧数据-复制的office文档-残留数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/delAllOldChapterLeave" ,method = {RequestMethod.GET,RequestMethod.POST})
	public ReturnResult<String> delAllOldChapterLeave(HttpServletRequest request,String key){
		if(!key.equals("yuyou1233212")){
			return ReturnResult.fail("key错误");
		}

		LoginRedisObj obj = this.getUserObj(request);
		if(!obj.getIsManager()){
			return ReturnResult.fail("你没有权限");
		}
		
    	//TODO 暂时
    	new Thread(new Runnable() {		
			@Override
			public void run() {						
				ResBoolSimpleInfo<String> res=lessonChapterService.delOldDataLeave();
			}
		}).start();
		

		return ReturnResult.success();
	}
	
	/**
	 * 资源base_chapter_id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/sycnResoreceChapter" ,method = {RequestMethod.GET,RequestMethod.POST})
	public ReturnResult<String> sycnResoreceChapter(HttpServletRequest request,String key){
		if(!key.equals("yuyou1233212")){
			return ReturnResult.fail("key错误");
		}

		LoginRedisObj obj = this.getUserObj(request);
		if(!obj.getIsManager()){
			return ReturnResult.fail("你没有权限");
		}

		ResBoolSimpleInfo<String> res=upGradeDataService.sycnResoreceType();
		
		return ReturnResult.returnRes(res);
	}
	
	@Autowired
	private MailAPI mailAPI;
	
    @RequestMapping(value = "/studentExcel", method = {RequestMethod.GET, RequestMethod.POST})
    public ReturnResult<String> examImport(@RequestParam(value="file") MultipartFile file,String key,
                                           HttpServletRequest request) throws Exception {
    	
		if(!key.equals("yuyou1233212")){
			return ReturnResult.fail("key错误");
		}

		LoginRedisObj obj = this.getUserObj(request);
		if(!obj.getIsSystem()){
			return ReturnResult.fail("你没有权限");
		}
    	
        InputStream inputStream = file.getInputStream();
        ImportParams params = new ImportParams();
        List<ReqMergeStuExcel> studentExcelList = ExcelImportUtil.importExcel(inputStream, ReqMergeStuExcel.class, params);
       
    	List<MergeStuDTO> dtoList=ListUtil.copyList(studentExcelList,MergeStuDTO.class);
     
        upGradeDataService.mergeStudent(dtoList);
        mailAPI.sendMonitorEmail("studentExcel","studentExcel ok");
        return ReturnResult.success("OK");
    }
	
	
}
